package com.davidk.survey.client.menu;

import com.davidk.survey.client.env.EnvironmentVariables;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.OperationBinding;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.types.DSOperationType;
import com.smartgwt.client.types.DSProtocol;

public class FunctionMenuDataSource extends DataSource {

	private static FunctionMenuDataSource instance = null;

	public static FunctionMenuDataSource getInstance() {
		if (instance == null) {
			instance = new FunctionMenuDataSource("fnMenuDS");
		}
		return instance;
	}

	public FunctionMenuDataSource(String id) {
		setID(id);
		setTitleField("Name");
		setDataFormat(DSDataFormat.JSON);
		setRecordXPath("response/data");
		setShowPrompt(false);
		
		DataSourceTextField nameField = new DataSourceTextField("Name", "Name", 128);
		nameField.setCanSortClientOnly(true);
		
		DataSourceTextField fnId = new DataSourceTextField("FunctionId", "Function Id");
		fnId.setPrimaryKey(true);
		fnId.setRequired(true);
		fnId.setDetail(true);

		DataSourceTextField parentId = new DataSourceTextField("ParentId", "ParentId");
		//parentId.setRequired(true);
		//parentId.setForeignKey(id + ".FunctionId");
		//parentId.setRootValue("1");
		parentId.setDetail(true);

		DataSourceTextField url = new DataSourceTextField("Type", "Type", 128);
		url.setDetail(true);

		DataSourceTextField data = new DataSourceTextField("Data", "Data", 128);
		data.setDetail(true);

		setFields(nameField, fnId, parentId, url, data);

		OperationBinding fetch = new OperationBinding();
		fetch.setOperationType(DSOperationType.FETCH);
		fetch.setDataProtocol(DSProtocol.GETPARAMS);
		setOperationBindings(fetch);
		setDataFormat(DSDataFormat.JSON);
		setDataURL(EnvironmentVariables.FunctionMenuURL);
		setClientOnly(true);
	}
}
